package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exection.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired//房间mapper
    private RoomInfoMapper roomInfoMapper;
    @Autowired//公寓mapper
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired//图片mapper
    private GraphInfoMapper graphInfoMapper;
    @Autowired//属性mapper
    private AttrValueMapper attrValueMapper;
    @Autowired//配套信息mapper
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired//标签mapper
    private LabelInfoMapper labelInfoMapper;
    @Autowired//支付方式mapper
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired//可选租期方式列表
    private LeaseTermMapper leaseTermMapper;

    @Autowired//出租表的service
    private LeaseAgreementService leaseAgreementService;
    @Autowired//图片service
    private GraphInfoService graphInfoService;

    @Autowired//房间属性service
    private RoomAttrValueService roomAttrValueService;

    @Autowired//房间配套service
    private RoomFacilityService roomFacilityService;
    @Autowired//房间标签service
    private RoomLabelService roomLabelService;
    @Autowired//房间支付方式service
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired//房间租期service
    private RoomLeaseTermService roomLeaseTermService;
    /**
     * 保存或更新房间信息
     *
     * @param submitVo
     */
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo submitVo) {
//        非空判断
        boolean boolSubmit = submitVo.getId() != null;
        super.saveOrUpdate(submitVo);//将接受的参数更新到room基本信息表中
        if (boolSubmit) {
            //1、删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getId, submitVo.getId());
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphQueryWrapper);
            //2、删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> attrKeyQueryWrapper = new LambdaQueryWrapper<>();
            attrKeyQueryWrapper.eq(RoomAttrValue::getId, submitVo.getId());
            roomAttrValueService.remove(attrKeyQueryWrapper);
            //3、删除配套信息列表
            LambdaQueryWrapper<RoomFacility> roomFacilityWrapper = new LambdaQueryWrapper<>();
            roomFacilityWrapper.eq(RoomFacility::getId, submitVo.getId());
            roomFacilityService.remove(roomFacilityWrapper);
            //4、删除标签列表
            LambdaQueryWrapper<RoomLabel> roomLabelWrapper = new LambdaQueryWrapper<>();
            roomLabelWrapper.eq(RoomLabel::getId, submitVo.getId());
            roomLabelService.remove(roomLabelWrapper);
            //5、删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeWrapper.eq(RoomPaymentType::getId, submitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeWrapper);
            //6、删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermWrapper.eq(RoomLeaseTerm::getId, submitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermWrapper);
        }
        //1、创建图片列表
        List<GraphVo> graphVoList = submitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setId(submitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //2、创建属性信息列表
        List<Long> attrValueIds = submitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            ArrayList<RoomAttrValue> attrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue build = RoomAttrValue
                        .builder()
                        .roomId(submitVo.getId())
                        .attrValueId(attrValueId)
                        .build();
                attrValues.add(build);
            }
            roomAttrValueService.saveBatch(attrValues);
        }
        //3、创建配套信息列表
        List<Long> facilityInfoIds = submitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility
                        .builder()
                        .roomId(submitVo.getId())
                        .facilityId(facilityInfoId)
                        .build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //4、创建标签列表
        List<Long> labelInfoIds = submitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel build = RoomLabel
                        .builder()
                        .roomId(submitVo.getId())
                        .labelId(labelInfoId)
                        .build();
                roomLabels.add(build);
            }
            roomLabelService.saveBatch(roomLabels);
        }
        //5、创建支付方式列表
        List<Long> paymentTypeIds = submitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId: paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType
                        .builder()
                        .roomId(submitVo.getId())
                        .paymentTypeId(paymentTypeId)
                        .build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //6、创建可选租期列表
        List<Long> leaseTermIds = submitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm
                        .builder()
                        .roomId(submitVo.getId())
                        .leaseTermId(leaseTermId)
                        .build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    /**
     * 根据条件分页查询room房间信息
     *
     * @param roomItemVoPage
     * @param roomQueryVo
     */
    @Override
    public IPage<RoomItemVo> PageSelectRoom(Page<RoomItemVo> roomItemVoPage, RoomQueryVo roomQueryVo) {
        return baseMapper.PageSelectRoom(roomItemVoPage,roomQueryVo);
    }

    /**
     * 根据id获取房间详情信息
     *
     * @param id
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //1.根据id查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //2.公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectByRoomId(id);
        //3.图片列表
        List<GraphVo> graphInfo = graphInfoMapper.selectByRoomId(ItemType.ROOM,id);
        //4.属性信息列表
        List<AttrValueVo> attrValueVos = attrValueMapper.selectByRoomId(id);
        //5.配套信息列表
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectByRoomId(id);
        //6.标签信息列表
        List<LabelInfo> labelInfos  =labelInfoMapper.selectByRoomId(id);
        //7.支付方式列表
        List<PaymentType>  paymentTypes= paymentTypeMapper.selectByRoomId(id);
        //8.可选租期方式列表
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectByRoomId(id);
        //9、拼接查询结果
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphInfo);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);
        return roomDetailVo;
    }

    /**
     * 根据id删除房间所有信息
     *
     * @param id
     */
    @Override
    public void removeRoomById(Long id) {

        long count = leaseAgreementService.count(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5, 7));
        if (count > 0) {
            //该房间正在出租，不能够删除
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCESS_ROOM_DELETE);
        }
        super.removeById(id);
        //1.删除房间支付方式
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        //2.删除房间属性值方式
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        //3.删除房间标签方式
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        //4.删除房间配套关系方式
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        //5.删除房间图片方式
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType,ItemType.ROOM).eq(GraphInfo::getItemId,id));
        //6删除可选租期列表
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));

    }
}




